<?php

include("../header.inc");
require_once("../DataBase.inc");
?>

<h2>Status Overview &nbsp;&nbsp;&nbsp;
     <span style="font-size:12px"><a href="import_amc.php">Import Result</a></span>
 </h2>


<?php

require_once($_SESSION["root_path"] . "/exams/exam_queries.inc");

$q = query_exam_centers_having_rooms_and_exam_plan();
$q->join("province", null, array("province.province_id=exam_center.province_id"));
$q->order("province.province_name");
$q->order("exam_center.exam_center_name");
$q->select("province.*");
$q->select("exam_center.*");
$exam_centers = $q->execute();

$subjects = DataBase::query("SELECT * FROM subject WHERE selection_campaign_id=" . $_SESSION["campaign_id"]);

echo "<table border=1>";
echo "<tr><th>Province</th><th>Exam Center</th><th>Room</th><th>Attendees</th><th>All Marks</th>";
foreach ($subjects as $subject)
    echo "<th>" . $subject["subject_name"] . "</th>";
echo "</tr>";

function query_attendees($center, $room) {
    $q = new Query("applicant");
    $q->join_applicant("applicant_exam_room_beta");
    $q->where("applicant_exam_room_beta.exam_center_id=" . $center["exam_center_id"]);
    $q->where("applicant_exam_room_beta.room_number=" . $room["room"]);
    $q->join_applicant("applicant_more_info");
    $q->where("applicant_more_info.applicant_more_info_attendee_exam_status=1");
    return $q;
}

foreach ($exam_centers as $center) {
    echo "<tr>";
    $rooms = DataBase::query("SELECT * FROM exam_center_rooms WHERE exam_center_id=" . $center["exam_center_id"]);
    echo "<td rowspan=" . count($rooms) . " valign=middle>" . $center["province_name"] . "</td>";
    echo "<td rowspan=" . count($rooms) . " valign=middle>" . $center["exam_center_name"] . "</td>";
    $first_room = true;
    foreach ($rooms as $room) {
        if ($first_room)
            $first_room = false; else
            echo "<tr>";
        echo "<td>" . $room["room"] . "</td>";
        $q = query_attendees($center, $room);
        $attendees = $q->count();
        echo "<td align=center>" . $attendees . "</td>";
        $q = query_attendees($center, $room);
        foreach ($subjects as $subject) {
            $q->join_applicant("applicant_score_subject_exam", "s" . $subject["subject_id"], array("s" . $subject["subject_id"] . ".subject_id=" . $subject["subject_id"]));
            $q->where("s" . $subject["subject_id"] . ".score_subject_exam IS NOT NULL");
        }
        $ok = $q->count();
        echo "<td align=center style='color:" . ($ok == $attendees ? "green" : "red") . "'>" . $ok . "</td>";
        foreach ($subjects as $subject) {
            $q = query_attendees($center, $room);
            $q->join_applicant("applicant_score_subject_exam", null, array("applicant_score_subject_exam.subject_id=" . $subject["subject_id"]));
            $q->where("applicant_score_subject_exam.score_subject_exam IS NOT NULL");
            $ok = $q->count();
            $q = query_attendees($center, $room);
            $q->join_applicant("applicant_score_subject_exam", null, array("applicant_score_subject_exam.subject_id=" . $subject["subject_id"]));
            $q->where("applicant_score_subject_exam.subject_version='A'");
            $va = $q->count();
            $q = query_attendees($center, $room);
            $q->join_applicant("applicant_score_subject_exam", null, array("applicant_score_subject_exam.subject_id=" . $subject["subject_id"]));
            $q->where("applicant_score_subject_exam.subject_version='B'");
            $vb = $q->count();
            echo "<td align=center style='color:" . ($ok == $attendees ? "green" : "red") . "'>" . $ok;
            echo " (";
            if ($va > 0 && $ok < $attendees)
                echo "<span style='color:blue'>";
            echo $va . "A";
            if ($va > 0 && $ok < $attendees)
                echo "</span>";
            echo ",";
            if ($vb > 0 && $ok < $attendees)
                echo "<span style='color:blue'>";
            echo $vb . "B";
            if ($vb > 0 && $ok < $attendees)
                echo "</span>";
            echo ")</td>";
        }
        echo "</tr>";
    }
}
echo "</table>";

include '../footer.inc';
?>

